﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using CourseSystem.Stat;
using CourseSystem.Stat.ActionFilter;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{

    public class StudyController : BaseController
    {
        //
        // GET: /Study/
        [StatFilter]
        [UrlStatFilter]
        [UserAuth]
        public ActionResult Index(int? id, int? iss)
        {
            ViewData["iss"] = "1";
            //如果是章
            if (!iss.HasValue)
            {
                var chapterInfo = ChapterBLL.GetLessOrderInfo(id);
                if (chapterInfo == null)
                    return NotFound();
                ViewData["iss"] = "0";
                ViewData["content"] = chapterInfo.CommonContentInfo.DetailContent;
                ViewData["id"] = chapterInfo.Id;
                ViewData["title"] = chapterInfo.Name;
                return View();
            }
            //若是节，必有id
            if (!id.HasValue)
                return NotFound();
            ViewData["id"] = id.Value;
            SectionInfo info = SectionBLL.Get(id.Value);
            if (info == null)
                return NotFound();
            //如果是学生就统计学习章节
            if (UserHelper.LoginUser.UserType == UserTypeEnum.Student)
            {
                LearnProcessStat.LearnSection(info.Id, UserHelper.LoginUser.Id, UserHelper.LoginUser.LoginTime);

                //更新学习该章节
                SectionLearnBLL.Learn(id.Value, UserHelper.Student.Id);
            }
            ViewData["content"] = info.CommonContentInfo.DetailContent;
            ViewData["title"] = info.ChapterInfo.Name + "  " + info.Name;
            return View();
            //var infoCourse = CourseStudyBLL.Get(id.Value, type.Value);
            //if (infoCourse != null)
            //    ViewData["content"] = infoCourse.CommonContentInfo.DetailContent;
            //return View();
            //return RedirectToAction("Course", new { sid = id.Value, type = type.Value });
        }
        [StatFilter]
        [UrlStatFilter]
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult Case()
        {
            ViewData["type"] = 1;
            return View();
        }
        [StatFilter]
        [UrlStatFilter]
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult CaseView(int id)
        {
            ViewData["type"] = 1;
            var info = CommonStudyBLL.Get(id);
            if (info == null)
                return NotFound();
            return View(info);
        }
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult CaseListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = CommonStudyBLL.GetList(0);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }
        [StatFilter]
        [UrlStatFilter]
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult PPT()
        {
            ViewData["type"] = 2;
            return View();
        }
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult PPTView(int id)
        {
            ViewData["type"] = 2;
            var info = CommonStudyBLL.Get(id);
            if (info == null)
                return NotFound();
            return View(info);
        }
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult PPTListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = CommonStudyBLL.GetList(1);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }
        [StatFilter]
        [UrlStatFilter]
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult Courseware()
        {
            ViewData["type"] = 3;
            return View();
        }
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult CoursewareView(int id)
        {
            ViewData["type"] = 3;
            var info = CommonStudyBLL.Get(id);
            if (info == null)
                return NotFound();
            return View(info);
        }
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult CoursewareListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = CommonStudyBLL.GetList(2);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }
        [StatFilter]
        [UrlStatFilter]
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult Prospect()
        {
            ViewData["type"] = 4;
            return View();
        }

        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult ProspectView(int id)
        {
            ViewData["type"] = 4;
            var info = CommonStudyBLL.Get(id);
            if (info == null)
                return NotFound();
            return View(info);
        }

        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult ProspectListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = CommonStudyBLL.GetList(3);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }


        [StatFilter]
        [UrlStatFilter]
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult Internet()
        {
            ViewData["type"] = 5;
            return View();
        }

        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult InternetListUc(int? pid, string search)
        {
            CommonRequest.ClearCache();
            var list = InternetResourcesBLL.GetList(search).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult ChapterTreeUc(int id, int iss)
        {
            ViewData["id"] = id;
            ViewData["iss"] = iss;
            ViewData["cid"] = id;
            if (iss == 1)
            {
                var info = SectionBLL.Get(id);
                if (info == null)
                    return NotFound();
                ViewData["cid"] = info.ChapterId;
            }

            var chapterList = ChapterBLL.GetList().OrderBy(o => o.OrderId);
            ViewData.Model = chapterList;
            return PartialView();
        }

    }
}
